package com.clothing.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.clothing.bean.BlackListJson;
import com.clothing.controller.BaseController;
import com.clothing.entity.BlackList;
import com.clothing.entity.User;
import com.clothing.service.BlackListService;
import com.clothing.service.UserService;
import com.clothing.utils.AjaxMsg;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @ClassName: UserController
 * @Description: 用户控制器
 * @author: Charles
 * @date: 2018/7/12 9:21
 * @version: 1.0
 */
@RequestMapping("/admin/user")
@Controller("adminUserController")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private BlackListService blackListService;

    /**
     * 添加黑名单
     *
     * @param userId
     * @param status
     * @param invalidDate
     * @return
     */
    @RequestMapping(value = "/addBlackList", method = RequestMethod.POST)
    @ResponseBody
    public String addBlackList(Long userId, BlackList.Status status, Date invalidDate) {
        if (null == invalidDate) {
            return AjaxMsg.failed("请输入失效时间");
        }
        if (null == status) {
            return AjaxMsg.failed("请选择用户状态");
        }
        User user = userService.find(userId);
        if (null == user) {
            return getParameterErrorMsg();
        }
        BlackList blackList = blackListService.findOne(userId);
        if (null == blackList) {
            blackList = new BlackList();
            blackList.setInvalidDate(invalidDate);
            blackList.setStatus(status);
            blackList.setUser(user);
            blackListService.save(blackList);
        } else {
            blackList.setInvalidDate(invalidDate);
            blackList.setStatus(status);
            blackList.setUser(user);
            blackListService.update(blackList);
        }
        return AjaxMsg.success("操作成功");
    }

    /**
     * 移除黑名单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/removeBlackList", method = RequestMethod.POST)
    @ResponseBody
    public String removeBlackList(Long id) {
        BlackList blackList = blackListService.find(id);
        if (null == blackList) {
            return AjaxMsg.success("操作成功");
        }
        blackListService.delete(blackList);
        return AjaxMsg.success("操作成功");
    }

    /**
     * 黑名单列表
     *
     * @param nickname
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/getBlackList.json", method = RequestMethod.GET)
    @ResponseBody
    public String blackList(String nickname, Pageable pageable) {
        Page<BlackList> page = blackListService.findPage(nickname, pageable);
        JSONArray ja = null;
        if (page.getTotal() > 0) {
            ja = new JSONArray();
            for (BlackList blackList : page.getContent()) {
                User user = blackList.getUser();
                BlackListJson json = new BlackListJson();
                json.setId(blackList.getId());
                json.setInvalidDate(blackList.getInvalidDate());
                json.setModifyDate(blackList.getModifyDate());
                json.setStatus(blackList.getStatus());
                json.setNickname(user.getNickname());
                ja.add(json);
            }
        }
        return new AjaxMsg(true, page.getTotal(), ja).toString();
    }
}
